<?php
/**
 * @author 	Vu Nhat Lam
 * 			Mail: lamvnd@gmail.com
 * 			Mobile: 0984 52 62 68
 * @version 1.0.0
 */
// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'btseedbonus');
define('CSRF_PROTECTION', true);

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array('user');

// pre-cache templates used by all actions
$globaltemplates = array(
	'USERCP_SHELL',
	'usercp_nav_folderbit'
);

// ########################## REQUIRE BACK-END ###########################
require_once('./global.php');
require_once(DIR . '/includes/functions_user.php');

// #######################################################################
// ######################### START MAIN SCRIPT ###########################
// #######################################################################
if (!($permissions['forumpermissions'] & $vbulletin->bf_ugp_forumpermissions['canview']))
{
	print_no_permission();
}

if (empty($vbulletin->userinfo['userid']))
{
	print_no_permission();
}

if ($_POST['do'] == 'transfer')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'points'    => TYPE_UINT,
		'recipient' => TYPE_STR
	));

	if (!$vbulletin->GPC['points'] || $vbulletin->GPC['recipient'] == '')
	{
		eval(standard_error(fetch_error('bt_bonus_transfer_miss_field')));
	}

	if ($vbulletin->GPC['points'] > $vbulletin->userinfo['seedbonus'])
	{
		eval(standard_error(fetch_error('bt_bonus_not_enable_point')));
	}

	$userlookup = htmlspecialchars_uni(strip_blank_ascii($vbulletin->GPC['recipient'], ' '));
	$userinfo = $db->query_first("
		SELECT userid
		FROM `" . TABLE_PREFIX . "user`
		WHERE username = '" . $db->escape_string($userlookup) . "'
	");

	if (!$userinfo || empty($userinfo['userid']) || $userinfo['userid'] == $vbulletin->userinfo['userid'])
	{
		eval(standard_error(fetch_error('bt_bonus_transfer_invalid_user')));
	}

	if ($db->query_write("UPDATE `" . TABLE_PREFIX . "user` SET seedbonus = seedbonus + {$vbulletin->GPC['points']} WHERE userid = {$userinfo['userid']}"))
	{
		$db->query_write("UPDATE `" . TABLE_PREFIX . "user` SET seedbonus = seedbonus - {$vbulletin->GPC['points']} WHERE userid = {$vbulletin->userinfo['userid']}");
		eval(print_standard_redirect('bt_bonus_transfer_successfully', true, true));
	}
	else
	{
		eval(print_standard_redirect('bt_bonus_transfer_unsuccessfully', true, true));
	}
}

if ($_POST['do'] == 'settitle')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'customtext' => TYPE_STR
	));

	if ($vbulletin->userinfo['seedbonus'] < $vbulletin->options['torrent_settitle_point'])
	{
		eval(standard_error(fetch_error('bt_bonus_not_enable_point')));
	}

	// init user data manager
	$userdata =& datamanager_init('User', $vbulletin, ERRTYPE_STANDARD);
	$userdata->set_existing($vbulletin->userinfo);

	if ($vbulletin->userinfo['usertitle'] != $vbulletin->GPC['customtext'] AND
		!($permissions['adminpermissions'] & $vbulletin->bf_ugp_adminpermissions['cancontrolpanel']) AND
		$vbulletin->options['ctMaxChars'] > 0
	)
	{
		// only trim title if changing custom title and not an admin
		$vbulletin->GPC['customtext'] = vbchop($vbulletin->GPC['customtext'], $vbulletin->options['ctMaxChars']);
	}

	// custom user title
	$userdata->set_usertitle(
		$vbulletin->GPC['customtext'], false,
		$vbulletin->usergroupcache[$vbulletin->userinfo['displaygroupid']], true,
		($permissions['adminpermissions'] & $vbulletin->bf_ugp_adminpermissions['cancontrolpanel']) ? true : false
	);

	// save the data
	if ($userdata->save())
	{
		$db->query_write("
			UPDATE `" . TABLE_PREFIX . "user`
			SET seedbonus = seedbonus - {$vbulletin->options['torrent_settitle_point']}
			WHERE userid = " . $vbulletin->userinfo['userid']
		);
	}

	eval(print_standard_redirect('bt_bonus_settitle_successfully', true, true));
}

if ($_POST['do'] == 'exchange')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'option' => TYPE_UINT
	));

	switch ($vbulletin->GPC['option'])
	{
		case '1':
			// 5 GB Upload
			if ($vbulletin->userinfo['seedbonus'] < $vbulletin->options['torrent_exchange_point_1'])
			{
				eval(standard_error(fetch_error('bt_bonus_not_enable_point')));
			}
			$db->query_write("
				UPDATE `" . TABLE_PREFIX . "user`
				SET uploaded = uploaded + 5368709120,
					seedbonus = seedbonus - {$vbulletin->options['torrent_exchange_point_1']}
				WHERE userid = " . $vbulletin->userinfo['userid']
			);
			break;
		case '2':
			// 10 GB Upload
			if ($vbulletin->userinfo['seedbonus'] < $vbulletin->options['torrent_exchange_point_2'])
			{
				eval(standard_error(fetch_error('bt_bonus_not_enable_point')));
			}
			$db->query_write("
				UPDATE `" . TABLE_PREFIX . "user`
				SET uploaded = uploaded + 10737418240,
					seedbonus = seedbonus - {$vbulletin->options['torrent_exchange_point_2']}
				WHERE userid = " . $vbulletin->userinfo['userid']
			);
			break;
		case '3':
			// 20 GB Upload
			if ($vbulletin->userinfo['seedbonus'] < $vbulletin->options['torrent_exchange_point_3'])
			{
				eval(standard_error(fetch_error('bt_bonus_not_enable_point')));
			}
			$db->query_write("
				UPDATE `" . TABLE_PREFIX . "user`
				SET uploaded = uploaded + 21474836480,
					seedbonus = seedbonus - {$vbulletin->options['torrent_exchange_point_3']}
				WHERE userid = " . $vbulletin->userinfo['userid']
			);
			break;
		case '4':
			// 50 GB Upload
			if ($vbulletin->userinfo['seedbonus'] < $vbulletin->options['torrent_exchange_point_4'])
			{
				eval(standard_error(fetch_error('bt_bonus_not_enable_point')));
			}
			$db->query_write("
				UPDATE `" . TABLE_PREFIX . "user`
				SET uploaded = uploaded + 53687091200,
					seedbonus = seedbonus - {$vbulletin->options['torrent_exchange_point_4']}
				WHERE userid = " . $vbulletin->userinfo['userid']
			);
			break;
		default:
			eval(standard_error(fetch_error('bt_bonus_exchange_invalid_option')));
			break;
	}

	eval(print_standard_redirect('bt_bonus_exchange_successfully', true, true));
}

// set shell template name
$shelltemplatename = 'USERCP_SHELL';
$includecss = array();

// initialise onload event
$onload = '';

// start the navbar
$navbits = array('usercp.php' . $vbulletin->session->vars['sessionurl_q'] => $vbphrase['user_control_panel']);

// draw cp nav bar
construct_usercp_nav('seedbonus');
$navbits['btseedbonus.php' . $vbulletin->session->vars['sessionurl_q']] = $vbphrase['bt_seed_bonus'];

$navbits[''] = $vbphrase['bt_seed_bonus'];
$page_templater = vB_Template::create('btseedbonus');

// make navbar
$navbits = construct_navbits($navbits);
$navbar = render_navbar_template($navbits);

// add any extra clientscripts
$clientscripts = (isset($clientscripts_template) ? $clientscripts_template->render() : '');

if (!$vbulletin->options['storecssasfile'])
{
	$includecss = implode(',', $includecss);
}

// shell template
$templater = vB_Template::create($shelltemplatename);
	$templater->register_page_templates();
	$templater->register('includecss', $includecss);
	$templater->register('cpnav', $cpnav);
	$templater->register('HTML', $page_templater->render());
	$templater->register('navbar', $navbar);
	$templater->register('navclass', $navclass);
	$templater->register('onload', $onload);
	$templater->register('pagetitle', $pagetitle);
	$templater->register('template_hook', $template_hook);
	$templater->register('clientscripts', $clientscripts);
print_output($templater->render());
